package g1gc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BigHumongousRegions {

    // 考你们一个问题：如果jdk11 G1GC下， 如果一个对象大小超过了region大小，会怎么样？
    // java -version: openjdk version "11.0.9" 2020-10-20
    // java -Xmx4g -Xms4g '-Xlog:gc*=info,heap*=info:HumongousRegions-gc.log:time,level,tags' g1gc.HumongousRegions 1000
    // 可以验证:
    // 100个2M+1长度的byte数组 ==> 放到大小为2M的region，需要2个 ==> Humongous regions: 200->200
    // 100个5M+1长度的byte数组 ==> 放到大小为2M的region，需要3个 ==> Humongous regions: 300->300

    public static void main(String[] args) throws IOException {
        // XMX:4G => region:2M => big object 1M+
        int h_size = 2*1024*1024+1; //可以改成5*1024*1024+1
        int h_count = Integer.parseInt(args[0]);
        List<byte[]> list = new ArrayList();

        // 第二个循环
        for (int i = 0;i<h_count;i++) {
            list.add(createHumongousBytes(h_size));
        }


        // keep running for tuning
        System.out.println("wait any keys...");
        int c = 0;
        while((c = System.in.read()) > 0){
            System.out.println("list size:" + list.size());
            if (c == 'q') {
                System.out.println("quit with q!"); break;
            } else {
                System.out.println("read a:" + c + "/" + (char) c);
            }
        }
    }

    static byte[] createHumongousBytes(final int size) {
        return new byte[size];
    }

}
